package screens;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import core.UIController;

public class ChangePasswordForm extends Form implements CommandListener {

	private static ChangePasswordForm instance;

	private Command commandVoltar;
	private Command commandEnviar;

	private TextField tfUsername;
	private TextField tfOldPassword;
	private TextField tfNewPassword;
	private TextField tfConfirmation;

	private ChangePasswordForm() {
		super("Mudança de Senha");

		this.commandEnviar = new Command("Enviar", Command.OK, 1);
		this.commandVoltar = new Command("Voltar", Command.BACK, 1);

		this.setCommandListener(this);

		this.tfUsername = new TextField("Nome de usuário", "", 8,
				TextField.PLAIN);
		this.tfOldPassword = new TextField("Senha atual", "", 8,
				TextField.PASSWORD);
		this.tfNewPassword = new TextField("Nova senha", "", 8,
				TextField.PASSWORD);
		this.tfConfirmation = new TextField("Confirmation", "", 8,
				TextField.PASSWORD);

		this.append(this.tfUsername);
		this.append(this.tfOldPassword);
		this.append(this.tfNewPassword);
		this.append(this.tfConfirmation);

		this.addCommand(this.commandEnviar);
		this.addCommand(this.commandVoltar);
	}

	public static Displayable getInstance() {
		if (instance == null) {
			instance = new ChangePasswordForm();
		}
		return instance;
	}

	public void commandAction(Command command, Displayable displayable) {
		try {
			if (command.equals(commandEnviar)) {
				UIController.getInstance().doChangePassword(
						tfUsername.getString(), tfOldPassword.getString(),
						tfNewPassword.getString(), tfConfirmation.getString());
			} else if (command.equals(commandVoltar)) {
				UIController.getInstance().voltar();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
